package pubsub;

import com.rabbitmq.client.*;
import org.junit.Test;
import util.ConnUtil;

import java.io.IOException;

/**
 * @author TZL
 * @date 2020/8/13
 */
public class Consumer {
    @Test
    public void consume1() throws Exception {
        Connection conn = ConnUtil.getConn();
        Channel channel = conn.createChannel();
        //3. declare....... Qos
        channel.basicQos(1);
        //4. consume
        DefaultConsumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("监听"+Publisher.QUEUE_NAME_1+"的消费者，获取到的消息："+new String(body,"UTF-8"));
                channel.basicAck(envelope.getDeliveryTag(),false);
            }
        };
        channel.basicConsume(Publisher.QUEUE_NAME_1,false,consumer);

        //5. close
        System.in.read();
        channel.close();
        conn.close();
    }

    @Test
    public void consume2() throws Exception {
        Connection conn = ConnUtil.getConn();
        Channel channel = conn.createChannel();
        //3. declare....... Qos
        channel.basicQos(1);
        //4. consume
        DefaultConsumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("监听"+Publisher.QUEUE_NAME_2+"的消费者，获取到的消息："+new String(body,"UTF-8"));
                channel.basicAck(envelope.getDeliveryTag(),false);
            }
        };
        channel.basicConsume(Publisher.QUEUE_NAME_2,false,consumer);

        //5. close
        System.in.read();
        channel.close();
        conn.close();
    }


}
